package view;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
//import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Statement;
import model.Dokument;
import model.OraConnection;
import model.PengendaliPenerimaSuratDokumen;
import model.Surat;
import oracle.jdbc.pool.OracleDataSource;
import sun.java2d.Disposer;

public class PanelTerimaSuratDokumen extends JPanel {

    private JLabel labelPanel, labelNomor, labelPerihal, labelAsal, labelPenerima,
            labelLetak, labelNama, labelTanggal;
    private JTextField textNomor, textPerihal, textAsal, textPenerima,
            textLetak, textNama, textTanggal;
    private JRadioButton radioSurat, radioDokumen;
    private JButton tombolSimpan, tombolBatal;

    public PanelTerimaSuratDokumen() {
        setLayout(null);

        labelPanel = new JLabel("Form Penerima Surat Dokumen");
        labelPanel.setBounds(150, 10, 200, 20);
        add(labelPanel);

        ButtonGroup group = new ButtonGroup();

        radioSurat = new JRadioButton("Surat");
        radioSurat.setBounds(130, 30, 60, 40);
        add(radioSurat);
        group.add(radioSurat);

        radioDokumen = new JRadioButton("Dokumen");
        radioDokumen.setBounds(200, 30, 170, 40);
        add(radioDokumen);
        group.add(radioDokumen);

        labelNomor = new JLabel("Nomor");
        labelNomor.setBounds(20, 80, 100, 20);
        add(labelNomor);

        textNomor = new JTextField(20);
        textNomor.setBounds(80, 80, 100, 20);
        add(textNomor);

        labelPerihal = new JLabel("Perihal");
        labelPerihal.setBounds(20, 110, 100, 20);
        add(labelPerihal);

        textPerihal = new JTextField(20);
        textPerihal.setBounds(80, 110, 100, 20);
        add(textPerihal);

        labelAsal = new JLabel("Asal");
        labelAsal.setBounds(20, 140, 100, 20);
        add(labelAsal);

        textAsal = new JTextField(20);
        textAsal.setBounds(80, 140, 100, 20);
        add(textAsal);

        labelPenerima = new JLabel("Penerima");
        labelPenerima.setBounds(20, 170, 100, 20);
        add(labelPenerima);

        textPenerima = new JTextField(20);
        textPenerima.setBounds(80, 170, 100, 20);
        add(textPenerima);

        labelLetak = new JLabel("Letak");
        labelLetak.setBounds(20, 200, 100, 20);
        add(labelLetak);

        textLetak = new JTextField(20);
        textLetak.setBounds(80, 200, 100, 20);
        add(textLetak);

        labelNama = new JLabel("Nama Petugas Penerima");
        labelNama.setBounds(20, 230, 150, 20);
        add(labelNama);

        textNama = new JTextField(20);
        textNama.setBounds(170, 230, 150, 20);
        add(textNama);

        labelTanggal = new JLabel("Tanggal Terima");
        labelTanggal.setBounds(20, 260, 150, 20);
        add(labelTanggal);

        textTanggal = new JTextField(20);
        textTanggal.setBounds(170, 260, 150, 20);
        add(textTanggal);

        tombolSimpan = new JButton("Simpan");
        tombolSimpan.setBounds(100, 300, 100, 30);
        add(tombolSimpan);
        tombolSimpan.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }
        });

        tombolBatal = new JButton("Batal");
        tombolBatal.setBounds(240, 300, 100, 30);
        add(tombolBatal);
        tombolBatal.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                tombolBatalAction();
            }
        });
        this.setSize(800, 400);
        this.setVisible(true);
    }

    public void tombolSimpanAction() {
        if (radioDokumen.isSelected()) {

        try {
                Dokument dok = new Dokument();
                dok.setAsalDokumen(textAsal.getText());
                dok.setNomorDokument(Integer.parseInt(textNomor.getText()));
                dok.setPenerimaDokumen(textNama.getText());
                dok.setPerihal(textPerihal.getText());
                dok.setPetugasPenerima(textPenerima.getText());
                dok.setTanggalTerima(textTanggal.getText());
                dok.setTempatPenyimpanan(textLetak.getText());
                
                PengendaliPenerimaSuratDokumen.tambahDokumen(dok);

//                JOptionPane.showMessageDialog(null, "Dokumen Disimpan");
                String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
                String userid = "MHS115314063";
                String password = "MHS115314063";
                Connection conn;
                OracleDataSource ds;
                ds = new OracleDataSource();
                ds.setURL(jdbcUrl);
                conn = ds.getConnection(userid, password);
                System.out.println("Koneksi is OK");
                Statement statement = conn.createStatement();
                String sql11 = "insert into dokumen(NO_DOKUMEN, perihal, asal_dokumen, penerima_dokumen, tanggal_terima, letak_dokumen, petugas_penerima)"
                        + " values ('" + Integer.parseInt(textNomor.getText()) + "'"
                        + ",'" + textPerihal.getText()+ "'"
                        + ",'" + textAsal.getText()+ "'"
                        + ",'" + textPenerima.getText()+ "'"
                        + ",'" + textTanggal.getText()+ "'"
                        + ",'"+ textLetak.getText()+ "'"
                        + ",'"+ textNama.getText()+ "')";
                System.out.println("values ('" + textNomor.getText() + "'"
                        + ",'" + textPerihal.getText() + "'"
                        + ",'" + textAsal.getText() + "'"
                        + ",'" + textPenerima.getText() + "'"
                        + ",'" + textTanggal.getText() + "'"
                        + ",'" + textLetak.getText() + "'"
                        + ",'" + textNama.getText() + "')");
//            System.out.println(textNomor.getText());

                statement.executeQuery(sql11);

                JOptionPane.showMessageDialog(null, "Dokumen Disimpan");
            } catch (SQLException ex) {
                Logger.getLogger(PanelTerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
            }
//            JOptionPane.showMessageDialog(null, "Dokumen Disimpan");
        } else {
            try {
//                PanelDaftarSurat daftarSurat = new PanelDaftarSurat();
                Surat surat = new Surat();
                PengendaliPenerimaSuratDokumen.tambahSurat(surat);
                surat.setAsalSurat(textAsal.getText());
                surat.setNomorSurat(Integer.parseInt(textNomor.getText()));
                surat.setPerihal(textPerihal.getText());
                surat.setPenerimaSurat(textPenerima.getText());
                surat.setPetugasPenerima(textNama.getText());
                surat.setTanggalTerima(textTanggal.getText());
                surat.setLetakSurat(textLetak.getText());
           
// dok.setAsalDokumen(textAsal.getText());
//                dok.setNomorDokument(Integer.parseInt(textNomor.getText()));
//                JOptionPane.showMessageDialog(null, "Surat Disimpan");
                String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
                String userid = "MHS115314063";
                String password = "MHS115314063";
                Connection conn;
                OracleDataSource ds;
                ds = new OracleDataSource();
                ds.setURL(jdbcUrl);
                conn = ds.getConnection(userid, password);
                System.out.println("Koneksi is OK");
                Statement statement = conn.createStatement();
                String sql11 = "insert into surat(NO_SURAT, perihal, asal_surat, penerima_surat, tanggal_terima, letak_surat, petugas_penerima)"
                        + " values ('" + Integer.parseInt(textNomor.getText()) + "'"
                        + ",'" + textPerihal.getText()+ "'"
                        + ",'" + textAsal.getText()+ "'"
                        + ",'" + textPenerima.getText()+ "'"
                        + ",'" + textTanggal.getText()+ "'"
                        + ",'"+ textLetak.getText()+ "'"
                        + ",'"+ textNama.getText()+ "')";
                System.out.println("values ('" + textNomor.getText() + "'"
                        + ",'" + textPerihal.getText() + "'"
                        + ",'" + textAsal.getText() + "'"
                        + ",'" + textPenerima.getText() + "'"
                        + ",'" + textTanggal.getText() + "'"
                        + ",'" + textLetak.getText() + "'"
                        + ",'" + textNama.getText() + "')");
//            System.out.println(textNomor.getText());

                
                statement.executeQuery(sql11);
               
                
//                daftarSurat.setData(surat);
//                statement.execute();
                JOptionPane.showMessageDialog(null, "Surat Disimpan");
            } catch (SQLException ex) {
                Logger.getLogger(PanelTerimaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void tombolBatalAction() {
        JOptionPane.showMessageDialog(null, "Berkas Tidak Disimpan");
        System.exit(0);
    }

//    @Override
//    public Object getValueAt(int rowIndex, int columnIndex) {
////        PanelTerimaSuratDokumen panel = new PanelTerimaSuratDokumen();
//        Dokument temp = getData().get(rowIndex);
//        if (columnIndex == 0) {
////            temp.setAsalDokumen();
//            return temp.getTanggalTerima();
//        } else if (columnIndex == 1) {
//            return temp.getNomorDokument();
//        } else if (columnIndex == 2) {
//            return temp.getAsalDokumen();
//        } else if (columnIndex == 3) {
//            return temp.getPerihal();
//        } else if (columnIndex == 4){
//            return temp.getPenerimaDokumen();
//        }else if(columnIndex == 5){
//            return temp.getLetakDokumen();
//        } else {
//            return temp.getPetugasPenerima();
//        }
//    }
}
